package com.ruoyi.app.service.impl;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.app.domain.ProductInfo;
import com.ruoyi.app.domain.ShopCustomer;
import com.ruoyi.app.service.IProductInfoService;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.MybatisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.app.mapper.ProductCategoryMapper;
import com.ruoyi.app.domain.ProductCategory;
import com.ruoyi.app.service.IProductCategoryService;
import org.springframework.util.CollectionUtils;

/**
 * 商品分类Service业务层处理
 * 
 * @author zkq
 * @date 2024-07-28
 */
@Service
public class ProductCategoryServiceImpl extends ServiceImpl<ProductCategoryMapper, ProductCategory> implements IProductCategoryService
{
    @Autowired
    private ProductCategoryMapper productCategoryMapper;

    @Autowired
    private IProductInfoService productInfoService;
    /**
     * 查询商品分类
     * 
     * @param id 商品分类主键
     * @return 商品分类
     */
    @Override
    public ProductCategory selectProductCategoryById(String id)
    {
        return baseMapper.selectById(id);
    }

    /**
     * 查询商品分类列表
     * 
     * @param productCategory 商品分类
     * @return 商品分类
     */
    @Override
    public List<ProductCategory> selectProductCategoryList(ProductCategory productCategory)
    {
        LambdaQueryWrapper<ProductCategory> lambdaQueryWrapper = new LambdaQueryWrapper<>(ProductCategory.class);
        Map<String, Object> params = productCategory.getParams();
        Object beginTime = params.get("beginTime");
        Object endTime = params.get("endTime");
        lambdaQueryWrapper.eq(ProductCategory::getShopId, productCategory.getShopId());
        lambdaQueryWrapper.between((beginTime!= null && endTime!= null), ProductCategory::getCreateTime, beginTime, endTime);
        lambdaQueryWrapper.orderByAsc(ProductCategory::getSort);
        return baseMapper.selectList(lambdaQueryWrapper);
    }

    /**
     * 添加或修改商品分类
     * @param productCategory
     * @return
     */
    @Override
    public boolean addOrUpdate(ProductCategory productCategory) {
        if(org.apache.commons.lang3.StringUtils.isEmpty(productCategory.getId())){
            return save(productCategory);
        } else {
            return updateById(productCategory);
        }
    }

    /**
     * 验证删除商品分类
     * @param productCategory
     * @return
     */
    @Override
    public boolean verifyRemove(ProductCategory productCategory) {
        // 查询还有没有绑定的商品
        List<ProductInfo> productInfos = productInfoService.selecProductCategoryById(productCategory.getId());
        if(CollectionUtils.isEmpty(productInfos)){
            throw new ServiceException("有绑定商品");
        }
        return removeById(productCategory.getId());
    }
}
